var a = getApp();

Page({
  data: {
    searchColor: "rgba(0,0,0,0.4)",
    searchSize: "16",
    searchName: "搜索商品",
    hidden: !1,
    curNav: 1,
    curIndex: 0,
    cateData: [],
    goods: [],
    checked: 0,
    selectId: 0,
    page: 1,
    getNextPage: true,
    loadPage: true,
    radioItems: [{ value: '不杀' }, { value: '杀好(要内脏)' }, { value: '杀好(不要内脏)' }],
    radioSelect: '',
    optionsHidden: true
  },
  onLoad: function () {
    var t = this;//, e = wx.getStorageSync("cate_data");

    /* if (e) {
      t.setData({
        cateData: e,
        curNav: e[0].id,
        selectId: e[0].id
      })
      t.getGoods();
    } else { */
    wx.request({
      url: a.apiUrl("category"),
      method: "post",
      header: {
        "Content-Type": "application/json"
      },
      success: function (a) {
        //wx.setStorageSync("cate_data", a.data.data), 
        t.setData({
          cateData: a.data.data,
          curNav: a.data.data[0].id,
          selectId: a.data.data[0].id
        });
        t.getGoods();
      }
    });
    //}


    // t.getGoods(t.data.cateData[0].cat_id[0].id);
  },
  onShow: function () {
    if (this.data.goods.length > 0) {
      this.asyncCartGoods()
    }
  },
  asyncCartGoods: function () {


    var token = wx.getStorageSync("token");
    let goods = this.data.goods;
    var that = this;

    wx.request({
      url: a.apiUrl("index/getCartGoods"),
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": token
      },
      success: function (t) {
        if (t.data.data.length == undefined) {
          t.data.data = [];
        }
        let cartGoods = t.data.data;
        goods.forEach((item, key) => {
          goods[key].cart_num = 0;
          cartGoods.forEach((val) => {
            if (val.goods_id == item.goods_id) {
              goods[key].cart_num = val.goods_number
            }

          })

        })

        that.setData({

          goods: goods
        });

      }
    });
  },
  radioChange: function (e) {
    var items = this.data.radioItems;
    for (var i = 0; i < items.length; ++i) {
      items[i].checked = items[i].value == e.detail.value
      items[i].checked && this.setData({
        radioSelect: items[i].value
      });
    }

    this.setData({
      radioItems: items
    });
  },
  closeFloat: function () {
    this.setData({
      optionsHidden: true
    })
  },
  goodsCheckout: function (t) {

    let type = t.currentTarget.dataset.type;
    let opt = t.currentTarget.dataset.opt;
    let index = t.currentTarget.dataset.index;
    let goods = this.data.goods;
    let that = this;
    if (type == 'jia') {
      if (goods[index].is_fish == 1 && opt != 1) {
        goods[index].index = index;
        that.setData({
          optionsHidden: false,
          fishGoods: goods[index]
        })
        return false;
      }
      if (opt == 1 && that.data.radioSelect == '') {
        wx.showToast({
          title: "请选择处理方式",
          image: "../../images/failure.png",
          duration: 2e3
        })
        return false;
      }
      if (opt == 1 && that.data.radioSelect != '') {
        t.remarks = that.data.radioSelect;
      }
      getApp().goodsCheckout_v2(t).then((res) => {
        if (res.data.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
            showCancel: false,
          })
        } else {
          that.setData({
            optionsHidden: true
          })
          goods[index].cart_num++;
          that.setData({
            goods: goods
          });
        }
      })
    } else {
      goods[index].cart_num--;
      if (goods[index].cart_num > 0) {
        getApp().goodsCartUpdate(t, goods[index].cart_num)
      } else {
        getApp().goodsCartDelete(t)

      }
      this.setData({
        goods: goods
      });
    }





  },
  selectNav: function (a) {
    var t = a.target.dataset.id, e = parseInt(a.target.dataset.index);
    self = this, this.setData({
      curNav: t,
      curIndex: e,
      scrollTop: 0,
      checked: 0,
      goods: [],
      selectId: t,
      page: 1,
      getNextPage: true
    });
    this.getGoods(t);
  },

  selectCate: function (a) {
    var that = this;
    var id = parseInt(a.target.dataset.id)
    that.setData({
      checked: parseInt(a.target.dataset.index),
      selectId: id,
      goods: [],
      page: 1,
      getNextPage: true

    })
    that.getGoods(id);

  },
  getGoods: function () {
    if (this.data.getNextPage == false || this.data.loadPage == false) {
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });

    this.setData({
      loadPage: false
    })
    var that = this;
    var r = getApp().apiUrl("goods/list")
    var g = wx.getStorageSync("token");
    var id = this.data.selectId
    var page = this.data.page;
    var currentPosition = wx.getStorageSync("currentPosition");

    wx.request({
      url: r,
      data: {
        area_id: id,
        page: page,
        per_page: 10,
        keyword: '',
        shop: 1,
        id: id,
        sort_key: "0",
        sort_value: "0",
        warehouse_id: "1",
        longitude: currentPosition.longitude,
        latitude: currentPosition.latitude
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": g
      },
      success: function (t) {
        // console.log(t)
        // if()
        if (t.data.data.length == undefined) {
          t.data.data = [];
        }
        var goods_data = that.data.goods

        that.setData({
          goods: goods_data.concat(t.data.data),
          page: page + 1,
          getNextPage: t.data.data.length > 0 ? true : false,
          loadPage: true
        })
        wx.hideLoading();

      }
    });
  },

  onShareAppMessage: function () {
    return {
      title: "乐乐买菜全部分类",
      desc: "乐乐买菜是一个围绕着老百姓餐桌的生鲜电商结合先上线下平台",
      path: "/pages/categroy/index"
    };
  }
});